package com.fellan.business;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.fellan.model.Line;
import com.fellan.model.Station;
import com.fellan.persistance.DBManager;
import com.fellan.persistance.PersistenceConstants;
import com.fellan.utils.DataUtils;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Edy
 * Date: 26.05.2012
 * Time: 15:53
 * To change this template use File | Settings | File Templates.
 */
public class DataManager {


    private IntentFilter receivedAllLinesSuccessIntentFilter;
    private IntentFilter receivedAllStationsSuccessIntentFilter;
    private IntentFilter receivedStationsForLinesSuccessIntentFilter;
    private IntentFilter receivedLinesForStationsSuccessIntentFilter;
    private IntentFilter receivedScheduleSuccessIntentFilter;

    private BroadcastReceiver receivedAllLinesSuccessReceiver;
    private BroadcastReceiver receivedAllStationsSuccessReceiver;
    private BroadcastReceiver receivedStationsForLinesSuccessReceiver;
    private BroadcastReceiver receivedLinesForStationsSuccessReceiver;
    private BroadcastReceiver receivedScheduleSuccessReceiver;

    private ArrayList<Line> allLinesList = new ArrayList<Line>();
    private ArrayList<Station> allStationsList = new ArrayList<Station>();
    private ArrayList<Line> linesForStationList = new ArrayList<Line>();
    private ArrayList<Station> stationsForLineList = new ArrayList<Station>();
    private ArrayList<Date> scheduleList = new ArrayList<Date>();

    /**
     * Single instance for this manager
     */
    private static DataManager instance;

    /**
     * The application context used to register receivers
     */
    private Context context;

    /**
     * Method used to return the single instance of this manager
     *
     * @return Instance of this manager
     */
    public static DataManager getInstance() {
        if (instance == null)
            instance = new DataManager();
        return instance;
    }

    /**
     * Private constructor
     */
    private DataManager() {
        //Get the context
        context = DataUtils.getApplicationContext();
    }

    public void requestAllLines()
    {
        if (receivedAllLinesSuccessIntentFilter == null)
            receivedAllLinesSuccessIntentFilter = new IntentFilter(PersistenceConstants.PC_ALL_LINES_RECEIVED_SUCCESSFULLY);
        if (receivedAllLinesSuccessReceiver == null)
            receivedAllLinesSuccessReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    context.unregisterReceiver(receivedAllLinesSuccessReceiver);

                    allLinesList = (ArrayList<Line>) DBManager.linesList;

                    context.sendBroadcast(new Intent(BusinessConstants.BC_ALL_LINES_RECEIVED_SUCCESSFULLY));
                }
            };
        context.registerReceiver(receivedAllLinesSuccessReceiver,receivedAllLinesSuccessIntentFilter);

        DBManager.getInstance().getAllLines();
    }

    public void requestAllStations() {
        if (receivedAllStationsSuccessIntentFilter == null)
            receivedAllStationsSuccessIntentFilter = new IntentFilter(PersistenceConstants.PC_ALL_STATIONS_RECEIVED_SUCCESSFULLY);
        if (receivedAllStationsSuccessReceiver == null)
            receivedAllStationsSuccessReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    context.unregisterReceiver(receivedAllStationsSuccessReceiver);

                    allStationsList = (ArrayList<Station>) DBManager.stationsList;

                    context.sendBroadcast(new Intent(BusinessConstants.BC_ALL_STATIONS_RECEIVED_SUCCESSFULLY));
                }
            };
        context.registerReceiver(receivedAllStationsSuccessReceiver,receivedAllStationsSuccessIntentFilter);

        DBManager.getInstance().getAllStations();
    }

    public void requestStationForLine(int line) {
        if(receivedStationsForLinesSuccessIntentFilter == null)
            receivedStationsForLinesSuccessIntentFilter = new IntentFilter(PersistenceConstants.PC_ALL_STATIONSBYLINE_RECEIVED_SUCCESSFULLY);
        if(receivedStationsForLinesSuccessReceiver == null)
            receivedStationsForLinesSuccessReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    context.unregisterReceiver(receivedStationsForLinesSuccessReceiver);
                    stationsForLineList = DBManager.stationsForLineList;
                    context.sendBroadcast(new Intent(BusinessConstants.BC_STATIONS_FOR_LINE_RECEIVED_SUCCESSFULLY));
                }
            };
        context.registerReceiver(receivedStationsForLinesSuccessReceiver, receivedStationsForLinesSuccessIntentFilter);

        DBManager.getInstance().getStationsForLine(line);
    }

    public void requestLinesForStation(int station)
    {
        if (receivedLinesForStationsSuccessIntentFilter == null)
            receivedLinesForStationsSuccessIntentFilter = new IntentFilter(PersistenceConstants.PC_ALL_LINESBYSTATION_RECEIVED_SUCCESSFULLY);
        if (receivedLinesForStationsSuccessReceiver == null)
            receivedLinesForStationsSuccessReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    context.unregisterReceiver(receivedLinesForStationsSuccessReceiver);
                    linesForStationList = DBManager.linesForStationList;
                    context.sendBroadcast(new Intent(BusinessConstants.BC_LINES_FOR_STATION_RECEIVED_SUCCESSFULLY));
                }
            };
        context.registerReceiver(receivedLinesForStationsSuccessReceiver,receivedLinesForStationsSuccessIntentFilter);

        DBManager.getInstance().getLinesForStation(station);
    }

    public void requestSchedule(int line, int station, DataUtils.Zile ziua) {
        if(receivedScheduleSuccessIntentFilter == null)
            receivedScheduleSuccessIntentFilter = new IntentFilter(PersistenceConstants.PC_SCHEDULE_RECEIVED_SUCCESSFULLY);
        if(receivedScheduleSuccessReceiver == null)
            receivedScheduleSuccessReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    context.unregisterReceiver(receivedScheduleSuccessReceiver);
                    scheduleList = DBManager.listDate;
                    context.sendBroadcast(new Intent(BusinessConstants.BC_SCHEDULE_RECEIVED_SUCCESSFULLY));
                }
            };
        context.registerReceiver(receivedScheduleSuccessReceiver,receivedScheduleSuccessIntentFilter);

        DBManager.getInstance().getSchedule(line, station, ziua);
    }

    public ArrayList<Line> getAllLinesList()
    {
        return allLinesList;
    }

    public ArrayList<Station> getAllStationsList() {
        return allStationsList;
    }

    public ArrayList<Station> getStationsForLineList() {
        return stationsForLineList;
    }

    public ArrayList<Line> getLinesForStationList() {
        return  linesForStationList;
    }

    public ArrayList<Date> getScheduleList() {
        return scheduleList;
    }
}
